﻿package net.sealuitk.gadgets 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Timer;
	import net.sealuitk.Container;
	import net.sealuitk.Gadget;
	import net.sealuitk.UIConst;
	/**
	 * ...
	 * @author mkn
	 */
	public class Grabbar extends ButtonClick {
		public static const LEFT:int = 0;
		public static const RIGHT:int = 1;
		
		private var buttonsLeft:Vector.<Button> = new Vector.<Button>();
		private var buttonsRight:Vector.<Button> = new Vector.<Button>();
		
		public function Grabbar(Name:String, BarGraphics:*, Height:Number = -1){
			super(Name, null, CLICK_HOLD);
			assignGraphics(UIConst.STATE_UP, BarGraphics);
			
			if (Height > 0) setSize(BarGraphics.width, Height);
			
			buildEventListener();
		}
		
		public function addButton(newButton:Button, toSide:int):void {
			if (toSide == LEFT) buttonsLeft.push(newButton);
			else if (toSide == RIGHT) buttonsRight.push(newButton);
			else throw Error("Unknown side: " + toSide);
			updateGraphics();
		}
		
		override protected function updateGraphics():void {
			super.updateGraphics();
			var b:int = 0;
			var xPos:int = padding.left;
			
			for (b = 0; b < buttonsLeft.length ; b++) {
				buttonsLeft[b].x = xPos;
				buttonsLeft[b].y = padding.top;
				xPos += buttonsLeft[b].width;
				addChild(buttonsLeft[b]);
			}
			
			xPos = padding.right;
			
			for (b = 0; b < buttonsRight.length ; b++) {
				xPos -= buttonsRight[b].width;
				buttonsRight[b].x = xPos;
				buttonsRight[b].y = padding.top;
				addChild(buttonsRight[b]);
			}
		}
		
		public function adaptToWindow():void {
			setSize(parentContainer.width, size.y);
		}
		
		override protected function mouseDown(e:MouseEvent):void {
			parentContainer.startDrag();
			super.mouseDown(e);
		}
		
		private function updateMask(e:Event):void {
			parentContainer.x = parentContainer.x;
			parentContainer.y = parentContainer.y;
		}
		
		override protected function mouseUp(e:MouseEvent):void {
			parentContainer.stopDrag();
			super.mouseUp(e);
		}
		
	}

}